package com.shangguigu.spirngioc.ioc;

import com.shangguigu.spirngioc.ioc.bean.Car;
import com.shangguigu.spirngioc.ioc.bean.Dog;
import com.shangguigu.spirngioc.ioc.bean.Person;
import com.shangguigu.spirngioc.ioc.bean.zuizi;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.core.env.ConfigurableEnvironment;

import java.util.Map;


@SpringBootApplication
public class SpirngIocApplication {

    public static void main(String[] args) {
        ConfigurableApplicationContext ioc = SpringApplication.run(SpirngIocApplication.class, args);
        System.out.println("========================IOC容器创建完成========================");

        zuizi zuizi=ioc.getBean(zuizi.class);
        System.out.println("zuizi = " + zuizi);
    }
    public static void test111(String[] args) {
        ConfigurableApplicationContext ioc = SpringApplication.run(SpirngIocApplication.class, args);
        Map<String, Person> bean=ioc.getBeansOfType(Person.class);
        System.out.println("bean = " + bean);

        //获取环境变量
        ConfigurableEnvironment environment = ioc.getEnvironment();
        String property = environment.getProperty("OS");
        System.out.println("property = " + property);

        //测试条件conditiononmissingbean来测试狗子
        Map<String,Dog> beansOfType = ioc.getBeansOfType(Dog.class);
        System.out.println("beansOfType = " + beansOfType);
    }

    public static void testfactory(String[] args) {
        ConfigurableApplicationContext ioc = SpringApplication.run(SpirngIocApplication.class, args);

        System.out.println("========================IOC容器创建完成========================");
//        UserController bean1= ioc.getBean(UserController.class);
//        System.out.println("bean1 = " + bean1);
//
//        UserService bean2 = ioc.getBean(UserService.class);
//        System.out.println("bean2 = " + bean2);
        Car bean3 = ioc.getBean(Car.class);
        System.out.println("bean3= " + bean3);

        Car bean4 = ioc.getBean(Car.class);
        System.out.println("bean4= " + bean4);

    }
    public static void test2(String[] args) {
        //ApplicationContext ,Spirng应用上下文对象，IOC容器
        ConfigurableApplicationContext ioc = SpringApplication.run(SpirngIocApplication.class, args);
        System.out.println("========================IOC容器创建完成========================");

        //获取IOC容器中所有的BeanDefinitionNames
//        for(Object o: ioc.getBeanDefinitionNames()){
//                System.out.println(o);
//        }

        //获取IOC容器中指定名称的Bean
        Object zhangsan = ioc.getBean("zhangsan");
        System.out.println(zhangsan);
        //获取指定类型的bean
        Object dog=ioc.getBean(Dog.class);
        System.out.println(dog);

        //获取所有bean
        for (String beanDefinitionName : ioc.getBeanDefinitionNames()) {
            System.out.println(beanDefinitionName);
        }
        }
    }

